import http from './http'

export interface LoginCredentials {
  email: string
  password: string
}

export interface RegisterData {
  username: string
  email: string
  password: string
  fullName: string
}

export interface User {
  _id: string
  username: string
  email: string
  fullName: string
  avatar?: string
  role: 'admin' | 'ops' | 'viewer'
  status: 'active' | 'inactive'
  notifications: {
    email: boolean
    webhook?: string
  }
  stats: {
    alertsHandled: number
    ticketsResolved: number
    lastLoginAt?: string
  }
  createdAt: string
  updatedAt: string
}

export interface AuthResponse {
  user: User
  accessToken: string
  refreshToken: string
}

/**
 * User login
 */
export const login = (credentials: LoginCredentials) => {
  return http.post<{ data: AuthResponse }>('/auth/login', credentials)
}

/**
 * User registration
 */
export const register = (data: RegisterData) => {
  return http.post<{ data: AuthResponse }>('/auth/register', data)
}

/**
 * Refresh access token
 */
export const refreshToken = (refreshToken: string) => {
  return http.post<{ data: { accessToken: string } }>('/auth/refresh', {
    refreshToken,
  })
}

/**
 * Get current user
 */
export const getCurrentUser = () => {
  return http.get<{ data: User }>('/auth/me')
}

/**
 * Update user profile
 */
export const updateProfile = (updates: Partial<User>) => {
  return http.put<{ data: User }>('/auth/profile', updates)
}

/**
 * Change password
 */
export const changePassword = (oldPassword: string, newPassword: string) => {
  return http.post('/auth/change-password', { oldPassword, newPassword })
}

/**
 * Logout
 */
export const logout = () => {
  return http.post('/auth/logout')
}
